package com.easy.dfcapi.service.impl;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.dfcapi.entity.ApiQuestion;
import com.easy.dfcapi.entity.ApiQuestionImg;
import com.easy.dfcapi.entity.ApiUser;
import com.easy.dfcapi.mapper.ApiQuestionImgMapper;
import com.easy.dfcapi.mapper.ApiQuestionMapper;
import com.easy.dfcapi.mapper.ApiUserMapper;
import com.easy.dfcapi.res.ImgRes;
import com.easy.dfcapi.res.QuestionRes;
import com.easy.dfcapi.res.Response;
import com.easy.dfcapi.service.ApiQuestionService;
import com.easy.dfcapi.utils.ApiSessionUser;
import com.easy.dfcapi.utils.ServletHelper;
import com.easy.dfcapi.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by VULCANTSeries on 2017/5/10.
 */
@Service
@Transactional(readOnly = true)
public class ApiQuestionServiceImpl implements ApiQuestionService {

    @Autowired
    private ApiQuestionMapper questionMapper;

    @Autowired
    private ApiQuestionImgMapper questionImgMapper;

    @Autowired
    private ApiUserMapper userMapper;

    @Override
    @Transactional(readOnly = false)
    public Result addQuestion(Response<QuestionRes> response) throws Exception{
        Result rt = new Result();
        try{
            Map<String,Object> map = new HashMap();
            map.put("images",ImgRes.class);
            QuestionRes data = response.getData(QuestionRes.class, map);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            Date now = new Date();
            ApiQuestion question = new ApiQuestion();
            question.setUser(user);

            question.setQuestionTitle(data.getTitle());
            //进行敏感词处理
            //question.setQuestionDesc(SensitivewordEngine.DealWithSensitiveword(sensitiveWordsMa).setQuestion(pper.selectAll(), Util.getRequestString(data.getQuestion())));
            question.setQuestionDesc(Util.getRequestString(data.getQuestion()));
            question.setQuestionType(1);// 问题
            question.setParentId(0);
            question.setIpAddress(response.getIp());
            question.preInsert(now);

            if(questionMapper.insert(question)>0){
                for (int i = 0; i < data.getImages().size(); i++) {
                    ImgRes img = data.getImages().get(i);
                    ApiQuestionImg questionImg = new ApiQuestionImg();
                    questionImg.setSortNo(i+1);
                    questionImg.setImgSno(i+1);
                    questionImg.setQuestionId(question.getQuestionId());
                    questionImg.setImgId(img.getImgId());
                    questionImg.setImgDesc(img.getImgDesc());
                    questionImgMapper.insert(questionImg);
                }
            }

            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result getQuestionList(Response<QuestionRes> response) {
        Result rt = new Result();
        try{
            QuestionRes data = response.getData(QuestionRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<Map<String,Object>> list = questionMapper.getQuestionList(data);
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result addAnswer(Response<QuestionRes> response) {
        Result rt = new Result();
        try{
            Map<String,Object> map = new HashMap();
            map.put("images",ImgRes.class);
            QuestionRes data = response.getData(QuestionRes.class, map);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            ApiQuestion ask = questionMapper.get(data.getQuestionId());
            if(ask == null){
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }

            Date now = new Date();
            ApiQuestion question = new ApiQuestion();
            question.setUser(user);
            question.setParentId(ask.getQuestionId());

            question.setQuestionTitle(data.getTitle());
            //进行敏感词处理
            //question.setQuestionDesc(SensitivewordEngine.DealWithSensitiveword(sensitiveWordsMa).setQuestion(pper.selectAll(), Util.getRequestString(data.getAnswer())));
            question.setQuestionDesc(Util.getRequestString(data.getAnswer()));
            question.preInsert(now);
            question.setIpAddress(response.getIp());
            question.setQuestionType(2);//回答

            if(questionMapper.insert(question)>0){
                for (int i = 0; i < data.getImages().size(); i++) {
                    ImgRes img = data.getImages().get(i);
                    ApiQuestionImg questionImg = new ApiQuestionImg();
                    questionImg.setSortNo(i+1);
                    questionImg.setImgSno(i+1);
                    questionImg.setQuestionId(question.getQuestionId());
                    questionImg.setImgId(img.getImgId());
                    questionImg.setImgDesc(img.getImgDesc());
                    questionImgMapper.insert(questionImg);
                }
            }
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;

    }

    @Override
    public Result getAnswerList(Response<QuestionRes> response) {
        Result rt = new Result();
        try{
            QuestionRes data = response.getData(QuestionRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            List<Map<String,Object>> list = questionMapper.getAnswerList(data);
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result addReply(Response<QuestionRes> response) {
        Result rt = new Result();
        try{
            Map<String,Object> map = new HashMap();
            map.put("images",ImgRes.class);
            QuestionRes data = response.getData(QuestionRes.class, map);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            ApiQuestion ask = questionMapper.get(data.getAnswerId());
            if(ask == null){
                rt.setStatus(EnumConfig.S_CODE.PARAM_ERROR);
                return rt;
            }

            Date now = new Date();
            ApiQuestion question = new ApiQuestion();
            question.setUser(user);
            question.setParentId(ask.getQuestionId());

            question.setQuestionTitle(data.getTitle());
            //进行敏感词处理
            //question.setQuestionDesc(SensitivewordEngine.DealWithSensitiveword(sensitiveWordsMa).setQuestion(pper.selectAll(), Util.getRequestString(data.getQuestion())));
            question.setQuestionDesc(Util.getRequestString(data.getReply()));
            question.preInsert(now);
            question.setIpAddress(response.getIp());
            question.setQuestionType(3);//回复

            if(questionMapper.insert(question)>0){
                for (int i = 0; i < data.getImages().size(); i++) {
                    ImgRes img = data.getImages().get(i);
                    ApiQuestionImg questionImg = new ApiQuestionImg();
                    questionImg.setSortNo(i+1);
                    questionImg.setImgSno(i+1);
                    questionImg.setQuestionId(question.getQuestionId());
                    questionImg.setImgId(img.getImgId());
                    questionImg.setImgDesc(img.getImgDesc());
                    questionImgMapper.insert(questionImg);
                }
            }
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }
        return rt;
    }

    @Override
    public Result getReplyList(Response<QuestionRes> response) {
        Result rt = new Result();
        try{
            QuestionRes data = response.getData(QuestionRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            List<Map<String,Object>> list = questionMapper.getReplyList(data);
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result getQuestionAll(Response<QuestionRes> response) {
        Result rt = new Result();
        try{
            QuestionRes data = response.getData(QuestionRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            List<Map<String,Object>> list = questionMapper.getQuestionAll(data);

            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result getQuestionDetail(Response<QuestionRes> response) {
        Result rt = new Result();
        try{
            QuestionRes data = response.getData(QuestionRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            Map<String,Object> ask = questionMapper.getQuestionDetail(data.getQuestionId());
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(ask);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;

    }

    @Override
    public Result getMyQuestionList(Response<QuestionRes> response) {
        Result rt = new Result();
        try{
            QuestionRes data = response.getData(QuestionRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            data.setUserId(user.getUserId());
            List<Map<String,Object>> list = questionMapper.getMyQuestionList(data);

            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result getMyAnswerQuestionList(Response<QuestionRes> response) {
        Result rt = new Result();
        try{
            QuestionRes data = response.getData(QuestionRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }
            data.setUserId(user.getUserId());
            List<Map<String,Object>> list = questionMapper.getMyAnswerQuestionList(data);

            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }
}
